﻿namespace Vkontakte.Api.Operations
{
	[ApiMethod("getGroupsFull")]
	[ApiMethod("getGroups")]
	public sealed class GroupsGet
		: Base30ApiOperation
	{
		private int[] _resultShort;
		private Group[] _groups;

		public GroupsGet()
		{
			Full = false;
		}

		protected override void ParseData(string data)
		{
			if (Full)
			{
				ListingResponce responce = Serializer.LoadObjectXmlString<ListingResponce>(data);
				_groups=responce.Groups;
			}
			else
			{
				int start = data.IndexOf('[');
				int end = data.IndexOf(']');
				string str = data.Substring(start + 1, end - start - 1);
				string[] strNums = str.Split(',');
				_resultShort = new int[strNums.Length];
				for (int i = 0; i < strNums.Length; i++)
				{
					_resultShort[i] = int.Parse(strNums[i]);
				}
			}
			
			base.ParseData(data);
		}

		protected override void Compile()
		{
			if (Full)
			{
				AddParameter("method", "getGroupsFull");
				
			}
			else
			{
				AddParameter("format", "json");
				AddParameter("method", "getGroups");
			}
			base.Compile();
		}

		public bool Full { get; set; }

		public int[] GetResultShort()
		{
			return _resultShort;
		}

		public Group[] GetResultLong()
		{
			return _groups;
		}
	}
}